package core;

import java.util.Arrays;

public class Optimization {
	//input variables
	protected DmuList dmuList;
	//runtime variables
	protected boolean isCalculated = false;
	//output variables
	private double[] optimizationWeights;
	private double[] relOptimizationWeights;
	private double optimizationObjective;
	public DmuList getDmuList() {
		return dmuList;
	}
	public void setDmuList(DmuList dmuList) throws CalculationException {
		if(!isCalculated){
			this.dmuList = dmuList;
		}
		else{
			throw new CalculationException(CalculationException.alreadyCalculatedMessage);
		}
	}
	public Optimization(DmuList dmuList) throws CalculationException {
		super();
		this.setDmuList(dmuList);
	}
	public void setOptimizationWeights(double[] optimizationWeights) {
		this.optimizationWeights = optimizationWeights;
	}
	public double[] getOptimizationWeights() throws CalculationException {
		if(isCalculated){
			return optimizationWeights;
		}
		else{
			throw new CalculationException(CalculationException.notYetCalculatedMessage);
		}
	}
	public double getOptimizationWeight(int index) throws CalculationException {
		if(isCalculated){
			return optimizationWeights[index];
		}
		else{
			throw new CalculationException(CalculationException.notYetCalculatedMessage);
		}
	}
	public double[] getRelOptimizationWeights() throws CalculationException {
		if(isCalculated){
			if(relOptimizationWeights == null){
				setRelOptimizationWeights();
			}
			return relOptimizationWeights;
		}
		else{
			throw new CalculationException(CalculationException.notYetCalculatedMessage);
		}
	}
	public double getRelOptimizationWeight(int index) throws CalculationException {
		if(isCalculated){
			if(relOptimizationWeights == null){
				setRelOptimizationWeights();
			}
			return relOptimizationWeights[index];
		}
		else{
			throw new CalculationException(CalculationException.notYetCalculatedMessage);
		}
	}
	private void setRelOptimizationWeights(){
		this.relOptimizationWeights = this.optimizationWeights.clone();
		for(int i = 0; i<optimizationWeights.length;i++){
			if(!(i >= optimizationWeights.length - this.dmuList.inputSize)) //only for outputs
			relOptimizationWeights[i] =optimizationWeights[i]/optimizationWeights[0];
			
		}
	}
	public void setOptimizationObjective(double optimizationObjective) {
		this.optimizationObjective = optimizationObjective;
	}
	public double getOptimizationObjective() throws CalculationException {
		if(isCalculated){
		return optimizationObjective;
		}
		else{
			throw new CalculationException(CalculationException.notYetCalculatedMessage);
		}
	}
	public void setCalculated() throws CalculationException{
			this.isCalculated = true;
	}
	public String toString(){
		if(isCalculated){
			this.setRelOptimizationWeights();
			return this.optimizationObjective + ";" + Arrays.toString(this.relOptimizationWeights);
		}
		else{
			return "DmuList.Cnt: " + dmuList.size() + " - isCalculated: false";
		}
	}
}
